
package info.openlvb.openface.client.base.person;

import java.util.Iterator;
import java.util.List;

import info.openlvb.openface.client.base.Group;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridRowListener;

public class ListGroupPanel extends VerticalPanel {

	Window window;
	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);
	List<Group> data;
	Store store = null;
	GridPanel grid;
	RecordDef recordDef;
	FlexTable table;
	SuggestBox sb;
	MultiWordSuggestOracle oracle2 = new MultiWordSuggestOracle();
	
	public ListGroupPanel(){
		setWidth("90%");
		recordDef = new RecordDef(
				new FieldDef[]{  
						new StringFieldDef("nom"),
						new StringFieldDef("description"),
						new StringFieldDef("rep_access")
				}  
		);
		store = new Store(recordDef);
		ldapService.searchGroups(new AsyncCallback<List<Group>>() {
			public void onFailure(Throwable caught) {
				MessageBox.alert("probleme", "probleme de communication avec le backend");
			}
			public void onSuccess(List<Group> result) {
				for (Iterator<?> iterator = result.iterator();iterator.hasNext();) {
					Group group_cour = (Group) iterator.next();
					Record r = recordDef.createRecord(new Object[]{
							group_cour.getCn(),group_cour.getDescription(),group_cour.getGroupdirectory()
					});
					store.insert(0,r);
					store.commitChanges();
				}
			}			
		});
		ColumnConfig[] columns = new ColumnConfig[]{  
				//column ID is company which is later used in setAutoExpandColumn  
				new ColumnConfig("Nom", "nom", 150,true),
				new ColumnConfig("Description", "description", 250,true),
				new ColumnConfig("Répertoire de groupe", "rep_access", 150,true)
		};  
		ColumnModel columnModel = new ColumnModel(columns);
		grid = new GridPanel();  
		grid.setWidth("100%");
		grid.setStore(store);  
		grid.setColumnModel(columnModel);  

		grid.setFrame(true);  
		grid.setStripeRows(true);  
		grid.setWidth(600);  
		grid.setHeight(300);  
		grid.setTitle("Liste des groupes");

		grid.addGridRowListener(new GridRowListener() {

			public void onRowClick(GridPanel grid, int rowIndex, EventObject e) {
				Record record = grid.getStore().getRecordAt(rowIndex);
				MemberInGroupWindow migw = new MemberInGroupWindow(record,ListGroupPanel.this);
				migw.setVisible(true);
				migw.show();
				migw.center();
			}
			
			public void onRowContextMenu(GridPanel grid, int rowIndex,
					EventObject e) {
			}
			public void onRowDblClick(GridPanel grid, int rowIndex,
					EventObject e) {
			}
		});

		add(grid);
	}
	/**
	 * pour rafraichir automatiquement la liste des groupes
	 */
	public void refresh() {
		store.removeAll();
		ldapService.searchGroups(new AsyncCallback<List<Group>>() {
			public void onFailure(Throwable caught) {
				MessageBox.alert("probleme", "probleme de communication avec le backend");
			}
			public void onSuccess(List<Group> result) {
				for (Iterator<?> iterator = result.iterator();iterator.hasNext();) {
					Group group_cour = (Group) iterator.next();
					Record r = recordDef.createRecord(new Object[]{
							group_cour.getCn(),group_cour.getDescription(),group_cour.getGroupdirectory()
					});
					store.insert(0,r);
					store.commitChanges();
				}
			}			
		});

	}
	public FlexTable getUserTable() {
		return table;
	}
}
